using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace MosziNET.Web.Gallery.Controls
{
    /// <summary>
    /// Creates a control where the user can see an image and a link
    /// </summary>
    public class ImageLink : CompositeControl
    {
        #region / Private fields /

        HyperLink link;
        Image image;
        Literal title;

        #endregion / Private fields /

        #region / Constructors /

        /// <summary>
        /// Creates a new instance of the ImageButton class
        /// </summary>
        public ImageLink()
        {
            link = new HyperLink();
            image = new Image();
            title = new Literal();

            link.Controls.Add(image);
            link.Controls.Add(title);
        }

        /// <summary>
        /// Creates a new instance of the ImageButton control
        /// </summary>
        /// <param name="url">The url to navigate to when the button is pressed</param>
        /// <param name="imageUrl">The image to display</param>
        /// <param name="text">The caption to display</param>
        public ImageLink(string url, string imageUrl, string text) : this()
        {
            this.NavigateUrl = url;
            this.ImageUrl = imageUrl;
            this.Text = text;
        }

        #endregion / Constructors /

        #region / Public properties /

        /// <summary>
        /// Gets/sets the navigate url for the image button
        /// </summary>
        public string NavigateUrl
        {
            get
            {
                return this.link.NavigateUrl;
            }
            set
            {
                this.link.NavigateUrl = value;
            }
        }

        /// <summary>
        /// Gets/sets the image url for the image button
        /// </summary>
        public string ImageUrl
        {
            get
            {
                return this.image.ImageUrl;
            }
            set
            {
                this.image.ImageUrl = value;
            }
        }

        /// <summary>
        /// Gets/sets the caption of the image button
        /// </summary>
        public string Text
        {
            get
            {
                return this.title.Text;
            }
            set
            {
                this.title.Text = value;
            }
        }

        #endregion / Public properties /

        #region / Overrides /

        /// <summary>
        /// Creates the child controls
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            
            // add to the control collection my link
            this.Controls.Add(link);
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (String.IsNullOrEmpty(this.ImageUrl))
                this.image.Visible = false;

            if (String.IsNullOrEmpty(this.NavigateUrl))
                this.link.Visible = false;

            base.OnPreRender(e);
        }

        #endregion / Overrides /
    }
}
